package com.huawei.hms.framework.network.restclient.hwhttp.okhttp;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.Headers;
import com.huawei.hms.framework.network.restclient.hianalytics.RequestFinishedInfo;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import com.huawei.hms.framework.network.restclient.hwhttp.Request;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestBody;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestTask;
import com.huawei.hms.framework.network.restclient.hwhttp.Response;
import com.huawei.hms.framework.network.restclient.hwhttp.ResponseBody;
import com.huawei.hms.framework.network.restclient.hwhttp.ResponseBody.Builder;
import com.huawei.hms.framework.network.restclient.hwhttp.model.ConnectionInfo;
import com.huawei.hms.framework.network.restclient.hwhttp.trans.OkRequestBody;
import java.io.IOException;
import java.net.ProtocolException;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
import o.ftz;
import o.fui;
import o.fup;
import o.fus.a;
import o.fuv;
import o.fuw;
import o.fuy;
import o.fvb;
import o.fvt;

public class OkRequestTask implements RequestTask {
    private static final String TAG = "OkRequestTask";
    private ftz call;
    private volatile boolean canceled;
    private boolean executed;
    private fuv okHttpClient;
    private Request request;

    public OkRequestTask(fuv o_fuv) {
        this.okHttpClient = o_fuv;
    }

    public Request request() {
        return this.request;
    }

    public Response execute(Request request) throws IOException {
        fuw okRequestBody;
        fup o_fup = null;
        synchronized (this) {
            if (this.executed) {
                throw new IllegalStateException("Already executed.");
            }
            this.executed = true;
        }
        this.request = request;
        a aVar = new a();
        String method = request.getMethod();
        RequestBody body = request.getBody();
        if (body != null) {
            if (HttpContants.HTTP_METHOD_GET.equals(method)) {
                method = "POST";
            } else if (!fvt.ˏ(method)) {
                throw new ProtocolException(method + " does not support writing");
            }
            Logger.v(TAG, "the length of body is " + request.getBody().body().length);
            if (body.body().length == 0) {
                okRequestBody = new OkRequestBody(request.getBody());
            } else {
                if (request.getBody().contentType() != null) {
                    o_fup = fup.ॱ(request.getBody().contentType());
                }
                okRequestBody = fuw.create(o_fup, request.getBody().body());
            }
        }
        int size = request.getHeaders().size();
        fui.a aVar2 = new fui.a();
        for (int i = 0; i < size; i++) {
            aVar2.ˊ(request.getHeaders().name(i), request.getHeaders().value(i));
        }
        aVar.ˎ(request.getUrl().getUrl()).ˎ(method, okRequestBody).ˏ(aVar2.ˊ()).ˋ(request.getConcurrentConnectEnabled());
        fuv okHttpClient = getOkHttpClient(request, aVar);
        if (request.isOnlyConnect()) {
            this.call = new OnlyConnectCall(okHttpClient, aVar.ॱ());
        } else {
            this.call = okHttpClient.ˎ(aVar.ॱ());
        }
        return parseOkResponse(this.call.execute());
    }

    public void cancel() {
        this.canceled = true;
        if (this.call != null) {
            this.call.cancel();
        }
    }

    public synchronized boolean isExecuted() {
        return this.executed;
    }

    public boolean isCanceled() {
        return this.canceled || (this.call != null && this.call.isCanceled());
    }

    public synchronized RequestFinishedInfo getRequestFinishedInfo() {
        RequestFinishedInfo requestFinishedInfo;
        HttpEventListener listener = HttpEventListener.getFactory().getListener(this.call);
        if (listener != null) {
            requestFinishedInfo = listener.getRequestFinishedInfo();
        } else {
            requestFinishedInfo = null;
        }
        return requestFinishedInfo;
    }

    public ConnectionInfo getConnectionInfo() {
        HttpEventListener listener = HttpEventListener.getFactory().getListener(this.call);
        if (listener != null) {
            return listener.getConnectionInfo();
        }
        return null;
    }

    public RequestTask clone() {
        return new OkRequestTask(this.okHttpClient);
    }

    private fuv getOkHttpClient(Request request, a aVar) {
        if (request.isClientConfigurationModified()) {
            return this.okHttpClient.ˉ().ˎ((long) request.getConnectTimeout(), TimeUnit.MILLISECONDS).ˊ((long) request.getReadTimeout(), TimeUnit.MILLISECONDS).ˏ((long) request.getPingInterval(), TimeUnit.MILLISECONDS).ˋ((long) request.getWriteTimeout(), TimeUnit.MILLISECONDS).ॱ((long) request.getConnectionAttemptDelay(), TimeUnit.MILLISECONDS).ˋ();
        }
        return this.okHttpClient;
    }

    private Response parseOkResponse(fvb o_fvb) {
        ResponseBody responseBody = null;
        fuy ᐝ = o_fvb.ᐝ();
        String ॱ = o_fvb.ʼ().ॱ(HttpContants.KEY_CONTENT_TYPE);
        fup ॱ2 = ॱ != null ? fup.ॱ(ॱ) : null;
        if (ᐝ != null) {
            Charset ॱ3;
            Builder contentLength = new Builder().inputStream(ᐝ.ˎ()).contentLength(ᐝ.ˏ());
            if (ॱ2 != null) {
                ॱ3 = ॱ2.ॱ();
            }
            responseBody = contentLength.charSet(ॱ3).contentType(ॱ2 != null ? ॱ2.ˏ() : "").build();
        }
        return new Response.Builder().body(responseBody).code(o_fvb.ˊ()).headers(parseOkHeaders(o_fvb.ʼ())).message(o_fvb.ˋ()).build();
    }

    private Headers parseOkHeaders(fui o_fui) {
        Headers.Builder builder = new Headers.Builder();
        int ˋ = o_fui.ˋ();
        for (int i = 0; i < ˋ; i++) {
            builder.add(o_fui.ॱ(i), o_fui.ˋ(i));
        }
        return builder.build();
    }
}
